package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.GroupUser;
import cn.com.blueInfo.bpm.person.mapper.GroupUserMapper;
import cn.com.blueInfo.bpm.person.repository.GroupUserRepository;
import cn.com.blueInfo.bpm.person.request.GroupUserDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 群组固定人员表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class GroupUserRepositoryImpl
        extends ServiceImpl<GroupUserMapper, GroupUser>
        implements GroupUserRepository, BaseRepository<GroupUser, GroupUserDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增群组固定人员
     * @param groupUser 群组固定人员参数
     */
    @Override
    public Integer createGroupUser(GroupUser groupUser) {
        return sqlExecuteResult(save(groupUser));
    }

    /**
     * 批量新增群组固定人员
     * @param groupUserList 群组固定人员参数列表
     */
    @Override
    public Integer batchCreateGroupUser(List<GroupUser> groupUserList) {
        return sqlExecuteResult(saveBatch(groupUserList), groupUserList.size());
    }

    /**
     * 删除群组固定人员
     * @param groupUserId 群组固定人员ID
     */
    @Override
    public Integer deleteGroupUser(Long groupUserId) {
        GroupUser groupUser = new GroupUser();
        groupUser.setId(groupUserId);
        groupUser.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(groupUser));
    }

    /**
     * 批量删除群组固定人员
     * @param groupUserIdList 群组固定人员ID列表
     */
    @Override
    public Integer batchDeleteGroupUser(List<Long> groupUserIdList) {
        List<GroupUser> groupUserList = new ArrayList<>();
        for (Long groupUserId : groupUserIdList) {
            GroupUser groupUser = new GroupUser();
            groupUser.setId(groupUserId);
            groupUser.setDeleted(BaseDeleted.DELETED);
            groupUserList.add(groupUser);
        }
        return sqlExecuteResult(removeBatchByIds(groupUserList), groupUserList.size());
    }

    /**
     * 更新部分群组固定人员
     * @param groupUser 群组固定人员参数
     */
    @Override
    public Integer updateGroupUserPartial(GroupUser groupUser) {
        validateUpdateId(groupUser.getId());
        LambdaUpdateWrapper<GroupUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GroupUser::getId, groupUser.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部群组固定人员
     * @param groupUser 群组固定人员参数
     */
    @Override
    public Integer updateGroupUser(GroupUser groupUser) {
        validateUpdateId(groupUser.getId());
        return sqlExecuteResult(updateById(groupUser));
    }

    /**
     * 查询一条群组固定人员
     * @param groupUserId 群组固定人员ID
     */
    @Override
    public GroupUser getGroupUserById(Long groupUserId) {
        LambdaQueryWrapper<GroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupUser::getId, groupUserId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询群组固定人员
     * @param groupUserQueryDTO 群组固定人员参数
     */
    @Override
    public Page<GroupUser> getGroupUserPage(GroupUserDTO.QueryDTO groupUserQueryDTO) {
        Page<GroupUser> groupUserPage = new Page<>(
                groupUserQueryDTO.getPage(), groupUserQueryDTO.getSize());
        LambdaQueryWrapper<GroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, groupUserQueryDTO);

        BaseDeleted deleted = groupUserQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(groupUserPage, queryWrapper);
        } else {
            return page(groupUserPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 群组固定人员 数量
     * @param groupUserQueryDTO 群组固定人员参数
     */
    @Override
    public Long getGroupUserCount(GroupUserDTO.QueryDTO groupUserQueryDTO) {
        LambdaQueryWrapper<GroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, groupUserQueryDTO);

        BaseDeleted deleted = groupUserQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 群组固定人员
     * @param groupUserQueryDTO 群组固定人员参数
     */
    @Override
    public List<GroupUser> getGroupUserList(GroupUserDTO.QueryDTO groupUserQueryDTO) {
        LambdaQueryWrapper<GroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, groupUserQueryDTO);

        BaseDeleted deleted = groupUserQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<GroupUser> queryWrapper, GroupUserDTO.QueryDTO groupUserQueryDTO) {

        Long groupId = groupUserQueryDTO.getGroupId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(groupId), GroupUser::getGroupId, groupId);

        Long identityId = groupUserQueryDTO.getIdentityId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(identityId), GroupUser::getIdentityId, identityId);

        addQueryWrapper(queryWrapper, groupUserQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<GroupUser, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                GroupUser::getOrderNumber,
                GroupUser::getTenantId,
                GroupUser::getApplicationId,
                GroupUser::getStatus,
                GroupUser::getDeleted,
                GroupUser::getDeleteUserId,
                GroupUser::getDeleteTime,
                GroupUser::getRemark,
                GroupUser::getCreateUserId,
                GroupUser::getCreateTime,
                GroupUser::getUpdateUserId,
                GroupUser::getUpdateTime,
                GroupUser::getExtend1,
                GroupUser::getExtend2,
                GroupUser::getExtend3);
    }

}
